import { Remember } from './../model/clerkDataContext';
import { Injectable } from '@angular/core';
import { Http, Headers, Jsonp, URLSearchParams } from '@angular/http';
import { ServiceBase } from "./serviceBase";
import { MD5 } from "../common/md5/md5";

@Injectable()
export class Authentication extends ServiceBase {
    constructor(protected http: Http, protected jsonp: Jsonp) { super(); }
    /**
     * 
     * @param userName
     * @param password
     * @param sucb
     * @param errcb
     */
    async storeLogin(username: string, password: string) {
        let url = "api/terminal/login/new";
        let postJson = {
            account: username,
            password: MD5.hex_md5(password)
        };
        this.StoreOwner = await this.PostCloud(url, postJson);

        return this.StoreOwner;
    }

    /**
     * 
     * @param userName
     * @param password
     * @param storeId
     */

    async employeeLogin(userName: string, password: string, checked: boolean) {
        let rememberList = this.RememberList; //记住密码数组
        let rememberResult = rememberList.find((item, index) => {
            return item.username == userName;
        });
        if (rememberResult) {
            if (rememberResult.password != password) {
                password = MD5.hex_md5(password);
            }
        } else {
            if (userName) {
                password = MD5.hex_md5(password);
            }
        }

        let url = "api/auth/login";
        let postJson = {
            account: userName,
            password: password,
            storeId: this.CurrentStore.id
        };

        let result = await this.Post(url, postJson);
        if (result) {
            if (result.token) {
                this.Token = result.token;
            }
            let user = {
                id: result.employee.userId,
                name: result.employee.userName,
                employeeNumber: result.employee.employeeNumber
            }
            this.CurrentUser = user;

            let remember = new Remember();
            remember.isRemember = checked;
            remember.username = userName;
            if (rememberResult) {
                rememberList.splice(rememberList.indexOf(rememberResult), 1);
                rememberResult.isRemember = checked;
                if (checked) {
                    if (rememberResult.password != password) {
                        rememberResult.password = password;
                    }
                } else {
                    rememberResult.password = "";
                }
                rememberList.push(rememberResult);
            } else {
                if (checked) {
                    remember.password = password;
                } else {
                    remember.password = "";
                }
                rememberList.push(remember);
            }
            this.RememberList = rememberList;
        }
        return result;
    }

    async getLDCConfig(storeId: string) {
        let url = "api/stores/" + storeId + "/config/get/config?type=system.ldc";
        let result: any = await this.GetCloud(url);
        let ldcconfig = result.value;
        if (ldcconfig && ldcconfig.host) {
            console.log(ldcconfig);
            this.LDCConfig = ldcconfig;
        }
        return this.LDCConfig;
    }

    employeeLogout() { }
}